package com.yaozheng.vocationaltraining.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.yaozheng.vocationaltraining.activity.NewsDetailActivity;
import com.yaozheng.vocationaltraining.activity.NewsDetailActivity_;
import com.yaozheng.vocationaltraining.utils.ErrorUtils;
import com.yaozheng.vocationaltraining.utils.JsonUtils;
import com.yaozheng.vocationaltraining.utils.LogUtils;
import com.yaozheng.vocationaltraining.utils.TypeUtils;

import net.sf.json.JSONObject;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by xiaoqing on 2016/8/2.
 */
public class MyPushReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
//        LogUtils.println("[MyReceiver] onReceive - " + intent.getAction() + ", extras: " + bundle);
        if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
            String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
            //send the Registration Id to your server...

        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
//            Log.d(TAG, "[MyReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));
//            processCustomMessage(context, bundle);

        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
//            Log.d(TAG, "[MyReceiver] 接收到推送下来的通知");
//            int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
//            Log.d(TAG, "[MyReceiver] 接收到推送下来的通知的ID: " + notifactionId);

        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
//            Log.d(TAG, "[MyReceiver] 用户点击打开了通知");
            //打开自定义的Activity
            try {
                Intent newsDetailIntent = new Intent(context, NewsDetailActivity_.class);
                newsDetailIntent.putExtra("title", "");
                JSONObject extraJsonObject = JsonUtils.getJsonObject(bundle.getString(JPushInterface.EXTRA_EXTRA));
                newsDetailIntent.putExtra("contentUrl", TypeUtils.getJsonString(extraJsonObject, "url"));
                newsDetailIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                context.startActivity(newsDetailIntent);
            } catch (Exception ex) {
                ErrorUtils.outErrorLog(ex);
            }
        }
    }

}
